package la.ge.server.servlets.gs2ge.ui;

import java.io.IOException;
import java.io.InputStream;
import java.io.StringWriter;
import java.net.URLEncoder;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import la.ge.server.GeServerContext;
import la.ge.server.servlets.URLEncodeInputUIServlet;
import la.ge.server.servlets.URLEncodeOutputUIServlet;
import la.ge.server.servlets.gs2ge.GetBigNodeGSResultServlet;
import la.ge.server.servlets.gs2ge.util.Util;
import la.gs.GSearchCriteria;

import org.apache.commons.io.IOUtils;

public class GEURLGeneratorServlet extends HttpServlet {

	/**
	 * request parameters:
	 * none
	 */
	@Override
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    	GeServerContext geServerContext = (GeServerContext) this.getServletContext().getAttribute( GeServerContext.ATTRIB_NAME );
        response.setContentType("text/html");
        response.setCharacterEncoding( "utf-8" );
        response.setStatus(HttpServletResponse.SC_OK);
        
        InputStream in = URLEncodeInputUIServlet.class.getClassLoader()
				.getResourceAsStream(
						"la/ge/server/servlets/gs2ge/ui/GEURLGeneratorServletInput.html");
		StringWriter stringWriter = new StringWriter();
		IOUtils.copy(in, stringWriter);
		in.close();
		String resp = stringWriter.toString();
		resp = resp.replace("${actionURL}", geServerContext
				.getServletPath( this.getClass() ));
		response.getWriter().println(resp);
	}
	
	/**
	 * request parameters:
	 * String fileType
	 * String keyword
	 */
	@Override
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    	GeServerContext geServerContext = (GeServerContext) this.getServletContext().getAttribute( GeServerContext.ATTRIB_NAME );
        response.setContentType("text/html");
        response.setCharacterEncoding( "utf-8" );
        response.setStatus(HttpServletResponse.SC_OK);

        String keyword = request.getParameter( "keyword" );
        if( keyword == null ) {
        	keyword = "";
        }
        String fileType = request.getParameter( "fileType" );
        if( keyword == null ) {
        	keyword = "";
        }

        InputStream in = URLEncodeInputUIServlet.class
				.getClassLoader()
				.getResourceAsStream(
						"la/ge/server/servlets/gs2ge/ui/GEURLGeneratorServletOutput.html");
		StringWriter stringWriter = new StringWriter();
		IOUtils.copy(in, stringWriter);
		in.close();
		String resp = stringWriter.toString();
		
		resp = resp.replace("${actionURL}", geServerContext.getServletPath(this
				.getClass()));
        
		String rawURL = 
			Util.genClientAccessURLPrefix(request) +
			geServerContext.getServletPath(GetBigNodeGSResultServlet.class)
				+ "?"
				+ Util.encodeSearchCriteria2URLQuery(new GSearchCriteria(
						fileType, keyword));
        resp = resp.replace("${result}", Util.URLQueryEncode( rawURL )  );

        response.getWriter().println( resp );
	}
	
	
	
}
